<?php

/*
 * main class of tg-php-gallery
 * has to be included, instantiated, and the methods called
 *
 * showThumbView
 * showSingleView
 * showAdminView
 */


require_once('config_standard.php');

class tg_php_gallery {

	var $wasHeaderSent = false;
	var $wasFooterSent = false;


	/**
	 * constructor
	 *
	 * @return 	void
	 */
	function __construct($activeGallery = NULL, $activeImage = NULL) {
		global $c_maxSingleWidth;
		global $c_maxSingleHeight;
		global $c_maxListWidth;
		global $c_maxListHeight;
		global $c_galleryWidth;
		global $c_imagesPerPage;
		$this->maxSingleWidth 	= $c_maxSingleWidth;
		$this->maxSingleHeight 	= $c_maxSingleHeight;
		$this->maxListWidth 	= $c_maxListWidth;
		$this->maxListHeight 	= $c_maxListHeight;
		$this->galleryWidth 	= $c_galleryWidth;
		$this->imagesPerPage 	= $c_imagesPerPage;

		if (isset($activeGallery)) {
			$this->gallery = new Gallery($activeGallery);
		}

	}


	/**
	 * displays the list (thumbnail) view
	 *
	 * @param 	string	name of uploaded field
	 * @return 	string	full path and filename of uploaded file
	 */
	function showListView($gallery) {

		if ($this->gallery->galleryName != $gallery) {
			$this->gallery->loadGalleryData($gallery);
		}
		$this->displayHeader($this->gallery->galleryTitle);

		$currentPage = ((int) $_GET['page'] > 0 ) ? (int) $_GET['page'] : 1;
		$imageCount = count($this->gallery->imageArraySorted);
		$imageArray = array();
		if ($imageCount > $this->imagesPerPage) {
			$imageArray = array_slice($this->gallery->imageArraySorted,($currentPage-1)*$this->imagesPerPage,$this->imagesPerPage);
			$pageBrowserHTML = $this->generatePageBrowser($imageCount, $this->imagesPerPage, $currentPage);
		} else {
			$imageArray = $this->gallery->imageArraySorted;
		}
        $singleViewURL = $_SERVER["SCRIPT_NAME"];

		require('lib/page.listView.php');

	}



	/**
	 * displays the single (full) view
	 *
	 * @param 	string	name gallery
	 * @param 	string	name image
	 * @return 	string	full path and filename of uploaded file
	 */
	function showSingleView($gallery, $image) {
		if ($this->gallery->galleryName != $gallery) {
			$this->gallery->loadGalleryData($gallery);
		}

        $currentImageKey = array_search($image, $this->gallery->imageArraySorted);

        ## is first image?
        if ($currentImageKey === 0) {
            $leftButton = '<img src="res/galerie_arrow_left_inact.gif" alt="No previous picture." title="No previous picture." border=0 />';
        } else {
            $leftButton = '<a href="' . $_SERVER["PHP_SELF"] . '?view=single&gallery=' . urlencode($gallery) . '&image=' . urlencode($this->gallery->imageArraySorted[$currentImageKey - 1]) . '"><img src="res/galerie_arrow_left.gif" alt="Previous picture." title="Previous picture." border=0 /></a>';
        }

        ## is last image?
        if ($currentImageKey === (count($this->gallery->imageArraySorted) - 1)) {
            $rightButton = '<img src="res/galerie_arrow_right_inact.gif" alt="No next picture." title="No next picture." border=0 />';
        } else {
            $rightButton = '<a href="' . $_SERVER["PHP_SELF"] . '?view=single&gallery=' . urlencode($gallery) . '&image=' . urlencode($this->gallery->imageArraySorted[$currentImageKey + 1]) . '"><img src="res/galerie_arrow_right.gif" alt="Next picture." title="Next picture." border=0 /></a>';
        }

		$this->displayHeader($this->gallery->galleryTitle);

		$caption = $this->gallery->image2captionArray[$image];

		## build image code
		$imageCode = '<img src="image.php?image=' . urlencode($image) . '&gallery=' . urlencode($gallery) . '&maxW=' . $this->maxSingleWidth . '&maxH=' . $this->maxSingleHeight . '" width="" height="" alt="' . $image . '" title="' . (($caption != '') ? $caption : $image) . '" />';

		require('lib/page.singleView.php');

	}



	/**
	 * displays the admin view
	 *
	 * @param 	string	name of uploaded field
	 * @return 	string	full path and filename of uploaded file
	 */
	function showAdminView() {

		$this->displayHeader('Admin View');

		require('lib/page.adminView.php');

	}


	/**
	 * generates HTML for Pagebrowser
	 *
	 * @param 	int		imageCount
	 * @param 	int		imagesPerPage
	 * @return 	string	html to display
	 */
	function generatePageBrowser($imageCount, $imagesPerPage, $activePage = 1) {
		if ((int) $imagesPerPage == 0) {
			$imagesPerPage = $this->imagesPerPage;
		}
		if ($imageCount <= $imagesPerPage) {
			return '';
		}
		if ((int) $activePage < 1) {
			$activePage = 1;
		}

		$newQueryString = '?';
		## get params from url
		if ($_SERVER["QUERY_STRING"] != '') {
			$queryArray = explode('&', $_SERVER["QUERY_STRING"]);
			foreach ($queryArray as $query) {
				$paramArray = explode('=', $query);
				if ($paramArray[0] == 'page') {
					continue;
				}
				$newQueryString .= $query . '&';
			}
		}

		$pageCount = ceil($imageCount / $imagesPerPage);

		if ($activePage > 1) {
			$prevLink = '<a href="' . $_SERVER["PHP_SELF"] . $newQueryString . 'page=' . ($activePage - 1) . '">&laquo;</a>';
		} else {
			$prevLink = '&laquo;';
		}

		if ($pageCount > $activePage) {
			$nextLink = '<a href="' . $_SERVER["PHP_SELF"] . $newQueryString . 'page=' . ($activePage + 1) . '">&raquo;</a>';
		} else {
			$nextLink = '&raquo;';
		}


		$html = '
		<div class="pageBrowser">
			<div class="browseLeft">' . $prevLink . '</div>
			<div class="pages">';

        for ($i=1; $i<=$pageCount; $i++) {
        	if ($i == $activePage) {
				$class = 'active';
				$link = $i;
        	} else {
				$class = 'inactive';
				$link = '<a href="' . $_SERVER["PHP_SELF"] . $newQueryString . 'page=' . $i . '">' . $i . '</a>';
        	}
			$html .= '<div class="pageNumber ' . $class . '">' . $link . '</div>';
		}


		$html .= '
       		</div>
			<div class="browseRight">' . $nextLink . '</div>
		</div>';

		return $html;
	}

	/**
	 * ouptuts page header
	 *
	 * @param 	string	page title
	 * @return 	void
	 */
	function displayHeader($title = 'Gallery') {
		if ($this->wasHeaderSent) {
			return;
		}
		$header = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<title>' . $title . '</title>
	<script type="text/javascript" src="lib/jquery-1.3.2.min.js"></script>
	<script type="text/javascript" src="lib/jquery.tablednd_0_5.js"></script>
</head>
<body>
';
		echo $header;
		$this->wasHeaderSent = true;
	}


	/**
	 * ouptuts page header
	 *
	 * @param 	string	name of gallery
	 * @return 	void
	 */
	function displayFooter($view = NULL) {
		if ($this->wasFooterSent) {
			return;
		}
		$footer = '</body>';
		echo $footer;
		$this->wasFooterSent = true;
	}
}

?>